<?
class DB
{
	var $info = array();    // store general info for connection
	var $plink;
	var $resource;
	var $query_cnt=0;
	var $query_cache=array();
		
	function db_connect()
	{    
		$this ->plink = mysqli_connect( $this->info['host'], $this->info['usr'], $this->info['pwd'] );
		if( !$this -> plink )
			die( 'Fail to connect: '. mysql_error() );
		$db_selected = mysql_select_db( $this->info['db'], $this -> plink );
		if( !$db_selected )
			die( "Can not use database: ". mysql_error() );

	}// end of db_connect()

	function db_select_db($db)
	{
		mysql_select_db($db);
	}
	function db_query( $sql, $dump = 0 )
	{   
		
		global $CONF;
	    
		if( $this -> info['prefix'] )
		{
			 $sql=str_replace('pw_',$this -> info['prefix'],$sql);  // for phpwind use only
		}else{
		     $CONF['sql_tbl_prefix']=='biz_' or $sql=str_replace('biz_',$CONF['sql_tbl_prefix'],$sql);
		}
		
		if( $dump ) echo $sql.'<br>';

		$this -> resource = mysql_query( $sql );
		
		if( !$this -> resource )
			die( "Invalid query: ".$sql." [". mysql_error()."]" );

		$this -> query_cnt++;

		$this -> query_cache[] = $sql;

		return $this -> resource;  // for some one happy
	}
    function db_query_first($sql)
    {
    	$this->db_query($sql." limit 0,1");
    	$r = $this->db_fetch_row();
    	return $r;
    }
	function db_query_list($sql)
    {
        $this->db_query($sql);
		$arr = array();
		while($r=$this->db_fetch_row($this->resource,MYSQL_ASSOC))
		{
			$arr[] = $r;
		}
    	return $arr;
    }
	function db_compile_fields( $array )
	{
		if( is_array($array))
		{
			$fields = "`".key( $array )."`";
			$value =  "'".current( $array )."'";
			next( $array );
			$t = count($array);
			for( $i=1; $i < $t ;$i++ )
			{
				$current_value = current( $array );
				$current_key = key( $array );
				$fields .=",`$current_key`";
				$value  .=",'$current_value'";
				next( $array );
			}
		}
		
	return $fields_value = array( 'fields' => $fields,'value' => $value );
	}
	
	function db_compile_update_fields($data) {
		
		$return_string = "";
		
		foreach ($data as $k => $v)
		{
			$v = preg_replace( "/'/", "\\'", $v );
			$return_string .= $k . "='".$v."',";
		}
		
		$return_string = preg_replace( "/,$/" , "" , $return_string );
		
		return $return_string;
	}
	/**
	 * 2010-09-01
	 */
	function db_fetch_fields(){
		return mysql_fetch_field($this->resource);
	}
	function db_fetch_row( $res ="",$t = MYSQL_ASSOC )
	{
		if( $res) return @mysql_fetch_array( $res ,$t);
		else return @mysql_fetch_array( $this->resource,$t );
	}
	function db_fetch_array()
	{
		$a = array();
		while($r=$this->db_fetch_row())
		{
			$a[] = $r;
		}
		return $a;
	}
    function db_fetch_createSql($table)
    {
 		$sql         = "show create table " . $table;
    	$get_sql     = mysql_query($sql ,$this->plink);
    	$result_soup = '';
    	if($row_table = mysql_fetch_row($get_sql)){
        	$result_soup .= $row_table[1] ;
    	}
    	return $result_soup;
    }
	function db_close()
	{
		mysql_close();
	}

	function db_fetch_num()
	{
	   return	mysql_num_rows($this->resource);
	}
	function db_insert_id()
	{
		return mysql_insert_id();
	}
	
	function db_affected_rows()
	{
		return mysql_affected_rows();
	}
    // add @ 2009-3-11
	function db_insert($sql) 
	{
		$this-> db_query($sql);
		return mysql_insert_id();
	}
	function delete($table,$conField,$ids)
	{
		if(empty($ids) || $ids=="")
		{
            echo "error ";
			return false;
		}
		$strIds = "";
		if(is_array($ids))
		{
			foreach($ids as $v)
			{
				$strIds = $strIds.",".$v;
			}	
			$strIds = substr($strIds,1);
		}
		else
		{
			$strIds = $ids;
		}
		$sql = "delete from $table where $conField in($strIds)";
		$this->db_query($sql);
		return true;
	}
    // add @ 2005-4-4
	function db_form_insert($table, $form_fields,$isTest=false)
	{
		
		// Build INSERT Query
		$count = 1;
		$fcount = sizeof($form_fields);
		foreach ($form_fields as $key => $value) {
			if ($count == $fcount) {
				// END Field Names
				$field_names .= "$key";

				// END Data
				$fields .= "'$value'";
			}
			else {
				// Field Names
				$field_names .= "$key, ";
			
				// Data
				$fields .= "'$value', ";
			}
			$count++;
		}	
		 $query = "INSERT INTO $table ($field_names) VALUES($fields)";
		if($isTest)
		{
			echo $query;
		}
		//$this->query($query);
		$this-> db_query($query);
		return mysql_insert_id();
	}
    // add @ 2005-4-13
	function db_form_update($table,$form_fields,$condition,$isTest=false)
	{
	
		// Make UPDATE query
		$count = 1;
		$fcount = sizeof($form_fields);
		foreach ($form_fields as $name => $value) {
			if ($count == $fcount) {
				// End Field Names
				$fields .= "$name='$value'";
			}
			else {
				$fields .= "$name='$value', ";
			}
			$count++;
		}	
		$query = "UPDATE $table SET $fields WHERE ".$condition;
		if($isTest)
		{
			echo $query;
		}
		$this -> db_query ($query);
		return true;
	}
	/**
	 * 先根据条件判断存在不存在 ,如果存在 更新 不存在 则插入
	 */
	function update_or_insert($table,$form_fields,$condition,$isTest=false)
	{
		$r=$this->db_query_first("select * from $table where $condition");
		if($r)
		{
			$this->db_form_update($table,$form_fields,$condition,$isTest);
		}
		else
		{
			$this->db_form_insert($table,$form_fields,$isTest);
		}
	}			
function getDBFields($arrNameValues,$prefix="db")
{
	$fields = null;
	foreach ($arrNameValues as $k => $v)
	{
		if (substr($k, 0, strlen($prefix)) == $prefix)
		{
			$fields[substr($k, strlen($prefix))] = $v;
		}
	}
	return $fields;
}
function getCondition($arr,$con="and",$eq="=")
{
	if($arr)
	{
		foreach($arr as $key =>$value)
		{
			if($value)
			{
				if($eq="like")
				{
					$conStr = $conStr."$key "."$eq"." '%$value%' ".$con." ";
				}
				else
				{
					$conStr = $conStr."$key "."$eq"." '$value' ".$con." ";
				}
			} 
		}
		$conStr=substr($conStr,0,strlen($conStr)-strlen($con)-1);
		return $conStr;
	}
	else
	{
		return null;
	}
}
}
?>